package cn.jane.bigevent.service.impl;

import cn.jane.bigevent.entity.User;
import cn.jane.bigevent.mapper.UserMapper;
import cn.jane.bigevent.service.UserService;
import cn.jane.bigevent.util.Md5Util;
import cn.jane.bigevent.util.ThreadLocalUtil;
import java.time.LocalDateTime;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    private final UserMapper userMapper;

    public UserServiceImpl(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public User findByUsername(String username) {
        return userMapper.findByUsername(username);
    }

    @Override
    public void register(String username, String password) {
        String md5Password = Md5Util.getMD5String(password);
        userMapper.add(username, md5Password);
    }

    @Override
    public void update(User user) {
        user.setUpdateTime(LocalDateTime.now());
        userMapper.update(user);
    }

    @Override
    public void updateAvatar(String avatarUrl) {
        userMapper.updateAvatar(avatarUrl, ThreadLocalUtil.getUserId());
    }

    @Override
    public void updatePassword(String newPassword) {
        userMapper.updatePassword(Md5Util.getMD5String(newPassword), ThreadLocalUtil.getUserId());
    }
}
